package com.xzx.guazi.service.impl;

import com.xzx.guazi.entity.GuaziCar;
import com.xzx.guazi.mapper.GuaziCarMapper;
import com.xzx.guazi.service.GuaziService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

@Service
public class GuziServiceImpl implements GuaziService {
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private GuaziCarMapper guziCarMapper;

    @Override
    public String getGuaziWebInfo() throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Cookie","uuid=170132f7-e4a3-49b3-8945-0a9e099e8d85; eba4b0c2-869c-4930-e836-5359f10c7777_views=1; financeCityDomain=jinhua; antipas=a54230NO52L9n6v353929795y5; user_city_id=115; ganji_uuid=8166746502302426570963; lg=1; close_finance_popup=2020-10-13; track_id=131681044441341952; cityDomain=zibo; clueSourceCode=%2A%2300; guazitrackersessioncadata=%7B%22ca_kw%22%3A%22-%22%7D; sessionid=11104dd0-3be7-4aef-95d6-e3cdb157e338; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22seo_baidu%22%2C%22ca_n%22%3A%22default%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22%22%2C%22ca_campaign%22%3A%22%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22ca_transid%22%3A%22%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22track_id%22%3A%22131681044441341952%22%2C%22display_finance_flag%22%3A%22-%22%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%22170132f7-e4a3-49b3-8945-0a9e099e8d85%22%2C%22sessionid%22%3A%2211104dd0-3be7-4aef-95d6-e3cdb157e338%22%2C%22ca_city%22%3A%22zibo%22%7D; Hm_lvt_936a6d5df3f3d309bda39e92da3dd52f=1602551801,1602558288; preTime=%7B%22last%22%3A1602566813%2C%22this%22%3A1593562109%2C%22pre%22%3A1593562109%7D; Hm_lpvt_936a6d5df3f3d309bda39e92da3dd52f=1602566815");
        httpHeaders.add("Host","www.guazi.com");
        httpHeaders.add("Sec-Fetch-Dest","document");
        httpHeaders.add("Sec-Fetch-Mode","navigate");
        httpHeaders.add("Sec-Fetch-Site","none");
        httpHeaders.add("Upgrade-Insecure-Requests","1");
        httpHeaders.add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpHeaders.add("Accept-Encoding","gzip, deflate, br");
        httpHeaders.add("Accept-Language","zh-CN,zh;q=0.9");
        httpHeaders.add("Connection","keep-alive");
        httpHeaders.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36");
        HttpEntity httpEntity = new HttpEntity(null,httpHeaders);
        for (int i = 1; i <= 50 ; i++) {
            stringBuilder.append("https://www.guazi.com/zibo/buy/o");
            stringBuilder.append(i);
            stringBuilder.append("/#bread");
            System.out.println(stringBuilder.toString());
            ResponseEntity<String> responseEntity = restTemplate.exchange(stringBuilder.toString(), HttpMethod.GET,httpEntity,String.class);
            Document document = Jsoup.parse(responseEntity.getBody());
            Elements elements = document.select("a.car-a");
            for (Element element : elements) {
                GuaziCar guaziCar = new GuaziCar();
                String str = element.attr("title");
                if (StringUtils.isEmpty(str)){
                    continue;
                }
                String[] strings = str.split(" ");
                if(str.indexOf("款") > -1){
                    guaziCar.setYearType(str.substring(str.indexOf("款")-4,str.indexOf("款")+1));
                }
                if (strings.length > 0){
                    for (int j = 0; j < strings.length; j++) {
                        guaziCar.setBrand(strings[0]);
                        if (strings[1].contains("款")){
                            guaziCar.setName(strings[0]);
                        }else {
                            guaziCar.setName(strings[1]);
                        }
                        if (strings[j].contains("国V")){
                            guaziCar.setNationalStandard(strings[j]);
                        }
                    }
                }
                guaziCar.setCarDetUrl(element.attr("href"));
                guaziCar.setDescription(str);
                Element element1 = element.selectFirst("img");
                Element element2 = element.selectFirst("div.t-i");
                Element element3 = element.selectFirst("div.t-price > p");
                Element element4 = element.selectFirst("div.t-price > em");

                if(Objects.nonNull(element4) && !StringUtils.isEmpty(element4.text())){
                    System.out.println("原价====>"+element4.text());
                    //原价
                    guaziCar.setOriginalPrice(element4.text().replace("万",""));
                }

                if(Objects.nonNull(element3) && !StringUtils.isEmpty(element3.text())){
                    System.out.println("现价====>"+element3.text());
                    //现价
                    guaziCar.setNowPrice(element3.text().replace("万",""));
                }



                System.out.println("上牌时间,里程数=====>"+element2.text());
                String[] strings1 = element2.text().split("\\|");
                if (strings1.length > 0){
                    for (int i1 = 0; i1 < strings1.length; i1++) {
                        guaziCar.setOnCardTime(strings1[0]);
                        guaziCar.setMilage(strings1[1]);
                    }
                }
                System.out.println("图片url====>"+element1.attr("src"));
                guaziCar.setImageUrl(element1.attr("src"));
                guziCarMapper.insert(guaziCar);
                System.out.println(guaziCar.toString());
            }
            stringBuilder.delete(0,stringBuilder.length());
        }
        return "str";
    }
}
